<?php
namespace Admin\Controller;
use Admin\Controller;
/**
 * 用户管理
 */
class MemberController extends BaseController
{
    /**
     * 用户列表
     * @return [type] [description]
     */
    public function index($key="")
    {
        if($key == ""){
            $model = M('member');
        }else{
            $model = M('member');
            if (filter_var($key, FILTER_VALIDATE_EMAIL)) {
                $where['username'] = trim($key);
                $model = $model->where($where);
            } else {
                $where['phone'] = trim($key);
                $model = $model->where($where);
            }
        }


        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $member = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
        $this->assign('member', $member);
        $this->assign('page',$show);//
        $this->display();
    }

    /**
     * 添加用户
     */
    public function add()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $this->fid = session('adminId');
            $config = M("Config")->find();

            $this->assign('config', $config);
            $this->display();
        }
        if (IS_POST) {
            //如果用户提交数据
            $model = D("Member");
            if (!$model->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($model->getError());
                exit();
            } else {
                if ($model->add()) {
                    $this->success("用户添加成功", U('member/index'));
                } else {
                    $this->error("用户添加失败");
                }
            }
        }
    }
    /**
     * 更新管理员信息
     * @param  [type] $id [管理员ID]
     * @return [type]     [description]
     */
    public function update()
    {
        //默认显示添加表单
        if (!IS_POST) {
            $model = M('member')->find(I('id'));
            $this->fid = session('adminId');
            $this->assign('model',$model);
            $this->display();
        }
        if (IS_POST) {

            $_POST['tianshu'] = strtotime($_POST['tianshu']);
            $model = D("Member");
            if (!$model->create()) {
                $this->error($model->getError());
            }else{
                //验证密码是否为空
                $data = I();
                unset($data['password']);
                if(I('password') !== ""){
                    $data['password'] = md5(I('password'));
                } else {
                    $data['password'] = $model->where(array('id'=>I('id')))->getField('password');
                }
                //强制更改超级管理员用户类型
                if(C('SUPER_ADMIN_ID') == I('id')){
                    $data['type'] = 2;
                }

                //给会员增加天数
                if ($_POST['gts'] == 0) {
                    $_POST['create_time'] =  time();
                }

                //var_dump($data);die;
                $info = $model->data($data)->save();
                //var_dump($info);die;
                //更新
                if ($info !== false) {
                    $this->success("用户信息更新成功", U('member/index'));
                } else {
                    $this->error("未做任何修改,用户信息更新失败");
                }
            }
        }
    }

    /**
     * 删除管理员
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        if(C('SUPER_ADMIN_ID') == $id) $this->error("超级管理员不可禁用!");
        $id=$this->jiexi($id);
        $status = I("type");
        $model = M('member');
        //查询status字段值
        if(!empty($status)){
            if($status == 1){
                $data['status']=0;
            }
            if($status == 0){
                $data['status']=1;
            }
        }else{
            $result = $model->find($id);


            //更新字段

            if($result['status'] == 1){
                $data['status']=0;
            }
            if($result['status'] == 0){
                $data['status']=1;
            }
        }
        if($model->where("id in($id)")->save($data)){
            $this->success("状态更新成功", U('member/index'));
        }else{
            $this->error("状态更新失败",U('member/index'));
        }
    }

    /**
     * 批量删除
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function alldel($id) {


        if (empty($id)) {
            $this->error("不能提交空值!");
        }
        //解析参数
        $id=$this->jiexi($id);
        $member = M("Member");
        $result = $member->where("id in($id)")->field()->select();
        $post = M('List');
        $jubao = M('jubao');
        $hobby = $post->where("fid in($id)")->field('id')->select();
        $arr = "";
        foreach ($hobby as $k => $val) {
            $arr .= $val['id'].',';
            $map['id'] = $val['id'];
            unlink('./Public/appipa/'.$val['id'].'.plist');
            unlink('./Public/png/'.$val['id'].'.png');
            //不知道啥用
            if ($result['uptype'] == 2) {
                $uptxt = $post->where(array('id'=>$val['id']))->getFiled('uptxt');
                unlink($uptxt);
            }
            $res = $post->where($map)->delete();
        }
        $jubaoId= rtrim($arr,',');
        if(!empty($jubaoId)){
        $rest = $jubao->where("appid in($jubaoId)")->field('id')->select();

            foreach ($rest as $k => $val) {

                $map['id'] = $val['id'];

                $res = $jubao->where($map)->delete();
            }
            }

        $memenum = $member->where("id in($id)")->delete($id);
        if($memenum){

            //unlink('./Public/uploads/'.$info['uptxt']);
            $this->success("删除成功", U('Member/index'));
        }else{
            $this->error("删除失败");
        }


    }

    function jiexi($id){
        //解析参数
        $arr = explode(",",$id);
        //如果有多余字符去掉
        $arr = array_merge(array_diff($arr, array("undefined")));
        $arr = array_merge(array_diff($arr, array("on")));

        if(is_array($arr)){
            return  $id = implode(',',$arr);
        }else{
            return   $id = 'id='.$arr;
        }
    }

}
